package com.zxx.pattern.proxyPattern.daili.dynamic;

import com.zxx.pattern.proxyPattern.daili.ChanelFactory;
import com.zxx.pattern.proxyPattern.daili.RedWineFactory;
import com.zxx.pattern.proxyPattern.daili.SellPerfume;
import com.zxx.pattern.proxyPattern.daili.SellWine;

import java.lang.reflect.Proxy;

/**
 * @author zxx on 2020/9/9
 */
public class XiaoMing2 {

    public static void main(String[] args) {
        //1. 实例化一个香水
        ChanelFactory factory = new ChanelFactory();
        //factory.sellPerfume(200);

        SellProxyFactory proxyFactory = new SellProxyFactory(factory);

        SellPerfume sellPerfume = (SellPerfume) Proxy.newProxyInstance(factory.getClass().getClassLoader(),
                factory.getClass().getInterfaces(), proxyFactory);
        sellPerfume.sellPerfume(1999.99);

        //----------------------------------------------------------
        //仅仅修改了此处，而没有修改代理工厂代码：
        //总结：修改了3处 1-添加接口，2-添加实现类，3-修改访问者

        //2 实例化一个红酒销售商
        RedWineFactory wineFactory = new RedWineFactory();
        //wineFactory.sellWine(201);

        // 实例化代理工厂，传入红酒销售商引用控制对其的访问
        SellProxyFactory sellProxyFactory = new SellProxyFactory(wineFactory);

        // 实例化代理对象，该对象可以代理售卖红酒
        SellWine sellWine = (SellWine) Proxy.newProxyInstance(wineFactory.getClass().getClassLoader(),
                wineFactory.getClass().getInterfaces(), sellProxyFactory);

        // 代理售卖红酒
        sellWine.sellWine(1999.888);
    }

    //在面对功能需求扩展时，只需要关注扩展的部分，不需要修改系统中原有的代码。


}
